import axios from 'axios'
export const getGreet = userName => {
  const timeNow = new Date();
  let hours = timeNow.getHours();
  let state = ``;
  if (hours >= 0 && hours <= 10) {
    state = `早上好,${userName}`;
  } else if (hours > 10 && hours <= 14) {
    state = `中午好,${userName}`;
  } else if (hours > 14 && hours <= 18) {
    state = `下午好,${userName}`;
  } else if (hours > 18 && hours <= 24) {
    state = `晚上好,${userName}`;
  }
  return state;
};


export const getDateData = () => {
  const dayName = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
  const date = new Date();
  const dateTime = date.getFullYear() + '-' + (date.getMonth() + 1) + '-' + date.getDate();
  const nowDay = dayName[date.getDay()];
  return {
    dateTime,
    nowDay
  }
}
// 根据指定长度一维数组分割成二维数组
export function splitArrayIntoChunks(arr = [], chunkSize) {
  const result = []
  if (arr.length === 0) {
    return []
  }
  for (let i = 0; i < arr.length; i += chunkSize) {
    result.push(arr.slice(i, i + chunkSize))
  }
  return result
}


export function getTypeName(type) {
  const typeObj = {
    app: '应用',
    model: '模型',
    report: '报告',
    target: '目标'
  }
  return typeObj[type] || ''
}

export function getActiveTypeName(index) {
  const typeObj = {
    0: 'app',
    1: 'model',
    2: 'target',
    3: 'report'
  }
  return typeObj[index]
}

// 根据文件类型，获取对应的接受上传的文件类型
export function getAcceptByType(type) {
  const typeObj = {
    image: 'jpg,.jpeg,.png',
    excel: '.xls,.xlsx',
    word: '.doc,.docx',
    ppt: '.ppt,.pptx',
    pdf: '.pdf',
    zip: '.rar,.zip,.7z,.tar,.gz,.bz2,.iso'
  }
  return typeObj[type] || ''
}
// 文件下载
export const downloadFileWay = (row) => {
  axios({
    method: 'get',
    url: row.ossFileDownUrl,
    responseType: 'blob'
  }).then(res => {
    const blob = new Blob([res.data], { type: 'application/octet-stream' })
    const url = window.URL || window.webkitURL || window
    const link = url.createObjectURL(blob)
    const a = document.createElement('a')
    a.href = link
    a.download = row.fileName
    document.body.appendChild(a)
    a.click()
    // 下载后移除a标签
    a.remove()
  })
}

export const getNumberFromString = (input) => {
  // 处理数字后缀
  const regex = /(\d+(\.\d+)?)([亿万]+)/;
  const match = input.match(regex);

  if (match) {
    let value = parseFloat(match[1]);  // 获取数字部分
    const unit = match[3];             // 获取单位部分

    // 根据单位进行转换
    switch (unit) {
      case '亿':
        value *= 1e8;  // 亿 -> 转换为单位数字
        break;
      case '万':
        value *= 1e4;  // 万 -> 转换为单位数字
        break;
      default:
        break;
    }

    return value;
  } else {
    // 如果没有单位，直接返回原始数字
    return parseFloat(input);
  }
}

// 示例2.63亿、5440万、23.5%，判断是否有对应的数字和符号
// 判断当前传入的字符串最后面是否有亿或者万
export const hasSuffix = (str) => {
  const suffixes = ['亿', '万']
  let status = 0
  if (str.endsWith('%')) {
    status = 1
  } else if (suffixes.some(suffix => str.endsWith(suffix))) {
    status = 2
  }
  return status
}

// export const hasSuffix = (str) => {
//   let status = 0
//   if (str.endsWith('亿') || str.endsWith('万')) {
//     status = 1
//   } else if (str.endsWith('%')) {
//     status = 2
//   }
//   return status
// }

// 去掉字符串后面的%
export const removePercentSymbol = (str) => {
  return str.replace(/%$/, '')
}